package mest.service.hws.impl;

import com.alibaba.fastjson.JSON;
import mest.dao.hws.BomSaveManytomanyDao;
import mest.dao.hws.plan_purchaseMapper;
import mest.entity.plan_productjob_purchase;
import mest.entity.plan_purchase;
import mest.service.cxj.DictionariesOrderService;
import mest.service.hws.Iplan_purchaseService;
import mest.service.hws.plan_productjob_purchaseService;
import mest.util.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Iplan_purchaseServiceImpl implements Iplan_purchaseService {
    @Resource
    private plan_purchaseMapper pp;

    @Resource
    private DictionariesOrderService dno;

    @Resource
    private BomSaveManytomanyDao bm;

    @Resource
    private plan_productjob_purchaseService ppp;

/*
* 1.新增计划采购单返回新增噶id，并字典表增加1
*  2.新增数据后返回ppid（新增数据id），proid查询关联物料存货多对多表多条存货档案，然后作为对象数组调用内容表新增方法
*/
    @Override
    public String insert(Map<String,Object> record) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            //判断是否有物料
            int i = pp.existBom(record);
            if (i < 1) {
                map.put("code", 0);
                map.put("msg", "没有物料无须生成");
                return JSON.toJSONString(map);
            }


            String theirDb = record.get("theirDb").toString();
            //自动生成流水号并set入对象
            System.out.println(dno.selectDictionariesOrder(record.get("theirDb").toString()).getPlanPurchaseNumber());
            record.put("tableNumber",("SJLCGD-"+dno.selectDictionariesOrder(record.get("theirDb").toString()).getPlanPurchaseNumber()));
           //直接生成时间
            System.out.println("时间"+DateUtil.getDateTimeToDate());
            Date time =  DateUtil.getDateTimeToDate();
            record.put("Time",time);
            System.out.println(record.toString());
            //新增计划采购单，返回id
            System.out.println("新增采购单-----------------");
            pp.insert(record);
            System.out.println("新增的采购单"+record);
            //新增成功后查询物料存货多对多关联poid的存货对象集合
            System.out.println("查询内容单所需的资料------------");
            List<plan_productjob_purchase> plan_productjob_purchaseList = bm.ppp(record.get("theirDb").toString(),record.get("poId").toString());
            //循环添加ppid到集合
            for (plan_productjob_purchase ppplist:plan_productjob_purchaseList){
                ppplist.setPpId(Integer.parseInt(record.get("id").toString()));
                System.out.println("每一条的内容单-------------"+ppplist);
//                ppplist.setDescription(record.getDescription());
            }
            //调用采购单内容生成接口，参数为新的对象组
            System.out.println("新增采购内容单-----------------");
            int  count = ppp.insert(record.get("theirDb").toString(),plan_productjob_purchaseList);
            if (count==0){
                map.put("code", 0);
                map.put("msg", "操作失败");
            }else {
                map.put("code", 0);
                map.put("msg", "操作成功,新增的采购单：1条---" + "采购内容表：" + count + "条");
                map.put("data", record);
                //操作成功后字典表+1
                dno.add_dictionaries_Order(theirDb,"planPurchaseNumber");
            }
        }catch (Exception e){
            System.out.println(e.toString());
            map.put("code", 1);
            map.put("msg", "操作失败");
        }
        return JSON.toJSONString(map);
    }

    //更新计划采购单
    @Override
    @RequestMapping("/updateByone")
    public String updateByPrimaryKey(String theirDb,
                                     plan_purchase record) {
        /*/insertModel /delModel /updateByone*/
        System.out.println("进入增删改方法");
        Map<String, Object> map = new HashMap<String, Object>();
        try {
                map.put("code", 0);
                map.put("msg", "操作成功");
                map.put("count", pp.updateByPrimaryKey(theirDb,record));
        }catch (Exception e){
            System.out.println(e.toString());
            map.put("code", 1);
            map.put("msg", "操作失败");
        }
        return JSON.toJSONString(map);
    }

    //根据id查询计划采购单
    @Override
    @RequestMapping("/selectById")
    public String selectByPrimaryKey(String theirDb,
                                     Integer id, Integer finishStatus) {
        /* selectById  selectAll  */
        System.out.println("进入查询");
        Map<Object,Object> map = new HashMap<Object,Object>();
        try {
                map.put("code",0);
                map.put("msg","查询成功");
                map.put("count",pp.selectcount(theirDb,finishStatus,id));
                map.put("data",pp.selectByPrimaryKey(theirDb,id,finishStatus));
        }catch (Exception e){
                map.put("code",1);
                map.put("msg","查询失败");
                map.put("errmsg",e.toString());
              System.out.println(e.toString());
        }
        return JSON.toJSONString(map);
    }

    //查询所有计划采购单
    @Override
    @RequestMapping("/selectAll")
    public Map<Object, Object> selectPrimary(String theirDb,
                                             Integer limit, Integer page, Integer finishStatus) {
        /* selectById  selectAll  */
        System.out.println("进入查询");
        Map<Object,Object> map = new HashMap<Object,Object>();
        try {
                map.put("code",0);
                map.put("msg","success");
                map.put("count",pp.selectcount(theirDb,finishStatus,null));
                map.put("data",pp.selectPrimary(theirDb,limit,(page-1)*limit,finishStatus));
        }catch (Exception e){
                map.put("code",1);
                map.put("msg","查询失败");
                map.put("errmsg",e.toString());
            System.out.println(e.toString());
        }
        return map;
    }

}
